package com.ctshk.rpc.scenic.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;
import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.DateUtil;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.scenic.dto.*;
import com.ctshk.rpc.scenic.entity.OperationLog;
import com.ctshk.rpc.scenic.entity.ScenicProduct;
import com.ctshk.rpc.scenic.generator.SHA256Util;
import com.ctshk.rpc.scenic.mapper.OperationLogMapper;
import com.ctshk.rpc.scenic.mapper.ScenicProductMapper;
import com.ctshk.rpc.scenic.req.OceanParkReq;
import com.ctshk.rpc.scenic.req.OperationLogListReq;
import com.ctshk.rpc.scenic.req.ScenicProductModifyRecommendStatusReq;
import com.ctshk.rpc.scenic.req.ScenicProductTicketsReq;
import com.ctshk.rpc.scenic.service.IOperationLogService;
import com.ctshk.rpc.scenic.service.OceanParkService;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.service.ISysUserService;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 操作日志表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-02-24
 */
@DubboService
public class OceanParkServiceImpl extends ServiceImpl<ScenicProductMapper, ScenicProduct> implements OceanParkService {

    private String agentId = "AT000005639";

    private String reqUrl = "https://otasuat.oceanpark.com.hk/otas-api";

    private String key = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJBVDAwMDAwNTYzOSIsImNyZWF0ZWQiOjE0Nzk3MDg2ODExMzIsImV4cCI6MTQ4MDMxMzQ4MX0.4U5Vjs2s9qwfy1vK2AL2iBH099MToV2dIlzvX_3Quy9CaHdGWwebuZ5L2ryIwPiuBnFw-EsjHPpedLRMTv4SPg";

    @Autowired
    ScenicProductMapper scenicProductMapper;

    @Override
    public Result getEvents(TokenUser currentUser) {
        String result = "";
        JSONObject param = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("agentId", agentId);
        jsonObject.put("requestTime", DateUtil.fmt(LocalDateTime.now().plusDays(1)));
        System.out.println(DateUtil.fmt(LocalDateTime.now().plusDays(1)));
        String mesage = jsonObject.toJSONString();
        String signature = SHA256Util.getSHA256String(mesage + "||" + key);
        param.put("message", jsonObject);
        param.put("signature", signature);
        try {
            result = HttpUtil.post(reqUrl + "/OTA/GetEvents", param);
            System.out.println(result);
        } catch (Exception e) {
            return null;
        }
        cn.hutool.json.JSONObject j = new cn.hutool.json.JSONObject(result);
        JSONArray events = j.getJSONArray("events");
        List<OceanParkEventsDTO> oceanParkEventsDTOS = (List<OceanParkEventsDTO>) JSON.parseArray(String.valueOf(events), OceanParkEventsDTO.class);
        if(CollectionUtils.isNotEmpty(oceanParkEventsDTOS)){
            for(OceanParkEventsDTO oceanParkEventsDTO : oceanParkEventsDTOS){
                ScenicProduct scenicProduct = new ScenicProduct();
                scenicProduct.setId(oceanParkEventsDTO.getEventId());
                cn.hutool.json.JSONObject jsonScenicProductName = new cn.hutool.json.JSONObject(oceanParkEventsDTO.getName());
                scenicProduct.setName(jsonScenicProductName.get("tc").toString());
                scenicProduct.setGmtCreate(LocalDateTime.now());
                scenicProduct.setCreateId(currentUser.getId());
                cn.hutool.json.JSONObject remark = new cn.hutool.json.JSONObject(oceanParkEventsDTO.getRemark());
                scenicProduct.setAdmissionNotes(remark.get("tc").toString());
                scenicProductMapper.insert(scenicProduct);
            }
        }
        return Result.success(oceanParkEventsDTOS);
    }

    @Override
    public Result<List<SpecificDateDTO>> getSpecificDates(TokenUser currentUser, OceanParkReq oceanParkReq) {
        String result = "";
        JSONObject param = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("agentId", agentId);
        jsonObject.put("requestTime", DateUtil.fmt(LocalDateTime.now()));
        jsonObject.put("eventId", oceanParkReq.getEventId());
        System.out.println(DateUtil.fmt(LocalDateTime.now().plusDays(1)));
        String mesage = jsonObject.toJSONString();
        String signature = SHA256Util.getSHA256String(mesage + "||" + key);
        param.put("message", jsonObject);
        param.put("signature", signature);
        try {
            result = HttpUtil.post(reqUrl + "/OTA/GetSpecificDates", param);
            System.out.println(result);
        } catch (Exception e) {
            return null;
        }
        cn.hutool.json.JSONObject j = new cn.hutool.json.JSONObject(result);
        JSONArray events = j.getJSONArray("specificDates");
        List<SpecificDateDTO> specificDateDTOS  = (List<SpecificDateDTO>) JSON.parseArray(String.valueOf(events), SpecificDateDTO.class);
        return Result.success(specificDateDTOS);
    }

    @Override
    public Result<List<ScenicProductTicketsDTO>> getTickets(TokenUser currentUser, ScenicProductTicketsReq scenicProductTicketsReq) {
        String result = "";
        JSONObject param = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("agentId", agentId);
        jsonObject.put("requestTime", DateUtil.fmt(LocalDateTime.now()));
        jsonObject.put("eventId", scenicProductTicketsReq.getEventId());
        jsonObject.put("specificDate", scenicProductTicketsReq.getSpecificDate());
        System.out.println(DateUtil.fmt(LocalDateTime.now().plusDays(1)));
        String mesage = jsonObject.toJSONString();
        String signature = SHA256Util.getSHA256String(mesage + "||" + key);
        param.put("message", jsonObject);
        param.put("signature", signature);
        try {
            result = HttpUtil.post(reqUrl + "/OTA/GetTickets", param);
            System.out.println(result);
        } catch (Exception e) {
            return null;
        }
        cn.hutool.json.JSONObject j = new cn.hutool.json.JSONObject(result);
        JSONArray tickets = j.getJSONArray("tickets");
        List<ScenicProductTicketsDTO> scenicProductTicketsDTOS  = (List<ScenicProductTicketsDTO>) JSON.parseArray(String.valueOf(tickets), ScenicProductTicketsDTO.class);
        return Result.success(scenicProductTicketsDTOS);
    }

    public static void main(String[] args) {
        TokenUser tokenUser = new TokenUser();
        tokenUser.setId(999l);
        OceanParkServiceImpl s = new OceanParkServiceImpl();

        //s.getEvents(tokenUser);
        OceanParkReq req = new OceanParkReq();
        req.setEventId("1237");
        s.getSpecificDates(tokenUser,req);
    }
}
